package se.webbzon.oschi01.worldcamera;

import javax.media.j3d.Transform3D;
import javax.media.j3d.View;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.sun.j3d.utils.universe.ViewingPlatform;

import se.webbzon.oschi01.friendly.Friendly;

@Friendly class SimpleWorldCamera implements WorldCamera {
	
	// The viewing platform of this world camera
	private final ViewingPlatform platform;
	
	/*============================================================
	Constructors
	============================================================*/
	
	@Friendly SimpleWorldCamera(int worldCameraProperties, ViewingPlatform platform) {
		this.platform = platform;
		if (isSet(worldCameraProperties,TRANSPARENCY))
			platform.getViewers()[0].getView().setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void projectPerspective(Point3d from, Point3d to, Vector3d up) {
		Transform3D t = new Transform3D();
		t.lookAt(from,to,up);
		t.invert();
		platform.getViewPlatformTransform().setTransform(t);
	}

	@Override public void setProjectionMinDistance(double minDistance) {
		platform.getViewers()[0].getView().setFrontClipDistance(minDistance);
	}

	@Override public void setProjectionMaxDistance(double maxDistance) {
		platform.getViewers()[0].getView().setBackClipDistance(maxDistance);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the given property is set in the given properties. **/
	private static final boolean isSet(int properties, int property) {
		return ((properties & property) == property);
	}

}
